#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run backups with dummy tape driver
# This test setups an Autochanger with 80 slots
# and 5 drives (3 LTO3 and 2 LTO1)
#
# This test is an "heavy" test with many jobs running at
# the same time, with copy jobs
 #
# TAPE_DRIVE="$cwd/working/ach/drive0"
# TAPE_DRIVE1="$cwd/working/ach/drive0"
# AUTOCHANGER="$cwd/working/ach/conf"
# USE_VTAPE=yes
# AUTOCHANGER_SCRIPT=disk-changer
# 

TestName="vtape-test2-changer"
JobName=backup
. scripts/functions

require_vtape

scripts/cleanup
scripts/copy-tape-confs
cp $rscripts/bacula-dir-vtape2.conf $conf/bacula-dir.conf
cp $rscripts/bacula-sd-vtape.conf $conf/bacula-sd.conf
scripts/prepare-fake-autochanger

echo "${cwd}/build" >${cwd}/tmp/file-list

start_test

when1=`perl -MPOSIX -e "print strftime('%F %T', localtime(time+30))"`
when2=`perl -MPOSIX -e "print strftime('%F %T', localtime(time+45))"`
when3=`perl -MPOSIX -e "print strftime('%F %T', localtime(time+60))"`


clientname=`awk '/Name = .*-fd/ { print $3 ; exit }' bin/bacula-dir.conf`

# Catalog record for cleaning tape "CLN01" successfully created.
# CLN01      | Cleaning

# Write out bconsole commands
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log6.out
setdebug level=150 trace=1 storage=LTO1
label barcodes pool=Scratch slots=1-40 storage=LTO1 drive=3
yes
messages
list volumes
END_OF_DATA

run_bacula

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log7.out
label barcodes pool=Scratch slots=41-80 storage=LTO3 drive=0
yes
messages
list volumes
@$out ${cwd}/tmp/log1.out

run storage=LTO1  level=Full job=NightlySave  pool=Inc     yes
wait
run storage=LTO1  when="$when1" job=copy-job  jobid=1  yes

run storage=LTO1 when="$when1" level=Full job=NightlySave  pool=Inc     yes
run storage=LTO1 when="$when1" level=Full job=NightlySave2 pool=Full    yes
run storage=LTO1 when="$when1" level=Full job=NightlySave1 pool=Default yes

run storage=LTO1 when="$when1" level=Full job=NightlySave  pool=Inc     yes
run storage=LTO1 when="$when1" level=Full job=NightlySave2 pool=Full    yes
run storage=LTO1 when="$when1" level=Full job=NightlySave1 pool=Default yes
run storage=LTO1 when="$when1" level=Full job=NightlySave3 pool=Diff    yes

wait
messages

run when="$when3" job=copy-job  jobid=1  yes
run when="$when3" job=copy-job  jobid=4  yes
run when="$when3" job=copy-job  jobid=5  yes
run when="$when3" job=copy-job  jobid=6  yes
run when="$when3" job=copy-job  jobid=7  yes
run when="$when3" job=copy-job  jobid=8  yes

run storage=LTO1  when="$when3" job=NightlySave2 level=Full pool=Full    yes
run storage=LTO1  when="$when3" job=NightlySave1 level=Full pool=Default yes

run storage=LTO1 when="$when3" level=Full job=NightlySave  pool=Inc     yes
run storage=LTO1 when="$when3" level=Full job=NightlySave2 pool=Full    yes
run storage=LTO1 when="$when3" level=Full job=NightlySave1 pool=Default yes
run storage=LTO1 when="$when3" level=Full job=NightlySave3 pool=Diff    yes

run storage=LTO1 when="$when3" level=Full job=NightlySave  pool=Inc     yes
wait jobid=22
status dir
status storage=LTO1
run storage=LTO1 when="$when3" job=copy-job  jobid=4    yes
run storage=LTO1 when="$when3" job=copy-job  jobid=5    yes
run storage=LTO1 when="$when3" job=copy-job  jobid=6    yes
run storage=LTO1 when="$when3" job=copy-job  jobid=7    yes
run storage=LTO1 when="$when3" job=copy-job  jobid=8    yes
run storage=LTO1 when="$when3" job=copy-job  jobid=22    yes
run storage=LTO1 when="$when3" job=copy-job  jobid=23    yes
wait

messages
quit
END_OF_DATA

run_bconsole
check_for_zombie_jobs storage=LTO1 client=$clientname
check_for_zombie_jobs storage=LTO3 client=$clientname

touch ${cwd}/build/po/*.po

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log2.out  
@# 
@# now do a restore
@#
restore client=$clientname fileset="Full Set" pool=Diff where=${cwd}/tmp/bacula-restores select all done
yes
wait
messages
wait
messages
END_OF_DATA

run_bconsole
check_for_zombie_jobs storage=LTO1 client=$clientname
check_for_zombie_jobs storage=LTO3 client=$clientname
check_for_zombie_jobs storage=LTO1-ANSI_6 client=$clientname

# test update slots
# remove volume vol35 from autochanger
grep -v vol35 ${cwd}/working/ach/barcodes > ${cwd}/tmp/1
cp ${cwd}/tmp/1 ${cwd}/working/ach/barcodes

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log3.out
list volume=vol35
update slots slots=15-40 storage=LTO1 drive=3
list volume=vol35
END_OF_DATA

run_bconsole

stop_bacula

check_two_logs
check_restore_diff

# get InChanger flag
RES=`awk -F'|' '/vol35.+Append/ { print $11 }' ${cwd}/tmp/log3.out`
set $RES
if [ $1 -eq $2 ]; then
    print_debug "ERROR: Error in update slots"
    bstat=1
fi

grep LTO1_5 tmp/log1.out > /dev/null
if test $? = 0; then
    print_debug "ERROR: AutoSelect option broken"
    bstat=1
fi

grep 'Error: Re-read' tmp/log1.out > /dev/null
if test $? = 0; then
    print_debug "ERROR: Found Re-read errors"
    bstat=1
fi

end_test
